﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Principal;
using System.Text;
using FlexiCommerce.Components;
using FlexiCommerce.Web;
using Rhino.Mocks;
using NUnit.Framework;

namespace FlexiCommerce.Tests
{
    public abstract class ItemTestsBase
    {
        protected MockRepository mocks;

        [SetUp]
        public virtual void SetUp()
        {
            mocks = new MockRepository();
        }

        [TearDown]
        public virtual void TearDown()
        {
            if (mocks != null)
            {
                mocks.ReplayAll();
                mocks.VerifyAll();
            }
        }

        protected virtual T CreateOneItem<T>(int id, string name, HierarchicalPersistenceItem parent) where T : PersistenceItem
        {
            T item = Activator.CreateInstance<T>();
            item.ID = id;
            if(!string.IsNullOrEmpty(name))
                item["Name"] = name;
            if (item is HierarchicalPersistenceItem)
                (item as HierarchicalPersistenceItem).AddTo(parent);
            return item;
        }


        protected IPrincipal CreatePrincipal(string name, params string[] roles)
        {
            return SecurityUtilities.CreatePrincipal(name, roles);
        }

        private Dictionary<string, object> requestItems;
        protected IWebContext CreateWebContext(bool replay)
        {
            requestItems = new Dictionary<string, object>();
            IWebContext context = mocks.StrictMock<IWebContext>();
            Expect.On(context).Call(context.RequestItems).Return(requestItems).Repeat.Any();

            if (replay)
                mocks.Replay(context);
            return context;
        }
    }
}
